package com.mxm.mirai.bot;

import com.mxm.mirai.service.BotgroupsService;
import com.mxm.mirai.tools.BotTools;
import net.dreamlu.mica.http.HttpRequest;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.message.GroupMessageEvent;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.message.data.MessageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

@Component
public class BotCreateCards implements Consumer<GroupMessageEvent> {
    @Autowired
    BotgroupsService botgroupsService;
    private static BotCreateCards botCreateCards;
    @PostConstruct
    public void init(){
        botCreateCards=this;
        botCreateCards.botgroupsService = this.botgroupsService;
    }
    private static BotTools botTools = new BotTools();
    private static BotCreateCardsData botCreateCardsData = new BotCreateCardsData();
    private static Map<String,Object> QQIdGroupId=new HashMap();
    @Override
    public void accept(GroupMessageEvent event) {
        if ("0".equals(botCreateCards.botgroupsService.QueryBotGroupsState(String.valueOf(event.getBot().getId()), String.valueOf(event.getGroup().getId()), "CreateCards"))) {
            return;
        }
        Group group = event.getGroup();//群类
        String message = event.getMessage().contentToString();//群消息
        long groupId = group.getId();//群号
        long qq = event.getSender().getId();//群发送人号
        botCreateCardsData = (BotCreateCardsData) QQIdGroupId.get(qq+""+groupId);
        botCreateCardsData = botCreateCardsData==null?botCreateCardsData = new BotCreateCardsData():botCreateCardsData;
        botCreateCardsData.setRunningQQ(qq);
        botCreateCardsData.setRunningGroup(groupId);
        switch (message){
            case "卡牌生成A":
                botCreateCardsData.setStep(1);
                QQIdGroupId.put(qq+""+groupId,botCreateCardsData);
                group.sendMessage("请发送要生成卡牌的图片！");
                break;
            case "卡牌生成B":
                botCreateCardsData.setStep(2);
                QQIdGroupId.put(qq+""+groupId,botCreateCardsData);
                group.sendMessage("请发送要生成卡牌的图片！");
                break;
            case "卡牌生成C":
                if(group.getOwner().getId()==2535080855L){
                botCreateCardsData.setStep(3);
                QQIdGroupId.put(qq+""+groupId,botCreateCardsData);
                group.sendMessage("请发送要生成卡牌的图片！");
                }
                break;
            default:
                stepB(event,qq);
                break;
        }
    }
    private void stepB(GroupMessageEvent event,long qq) {
        if (qq != botCreateCardsData.getRunningQQ() || event.getGroup().getId() != botCreateCardsData.getRunningGroup()) {
            return;
        }
        Group group = event.getGroup();//群类
        String messageB = event.getMessage().contentToString();//群消息
        String message = event.getMessage().toString();
        switch (botCreateCardsData.getStep()){
            case 1://A
                if (message.contains("[mirai:image:")) {
                    group.sendMessage("正在处理图片中...");
                    String url = StringUtils.substringBetween(message, "[mirai:image:", "]");
                    String imageUrl = group.getBot().queryImageUrl(MessageUtils.newImage(url));
                    botCreateCardsData.setImgid(botTools.getURLFile(imageUrl));
                    group.sendMessage("图片处理成功！\n请发送标题");
                    botCreateCardsData.setStep(4);
                }
                break;
            case 2://B
                if (message.contains("[mirai:image:")) {
                    group.sendMessage("正在处理图片中...");
                    String url = StringUtils.substringBetween(message, "[mirai:image:", "]");
                    String imageUrl = group.getBot().queryImageUrl(MessageUtils.newImage(url));
                    imageUrl = botTools.SmartMattingA(imageUrl);
                    if(!"".equals(imageUrl)) {
                        byte[] pic = HttpRequest.get(imageUrl)
                                .connectTimeout(Duration.ofSeconds(120))
                                .readTimeout(Duration.ofSeconds(120))
                                .execute()
                                .asBytes();
                        botCreateCardsData.setImgid(new ByteArrayInputStream(pic));
                        group.sendMessage("图片处理成功！\n请发送标题");
                        botCreateCardsData.setStep(4);
                    }else {
                        group.sendMessage("图片处理失败！");
                    }
                }
                break;
            case 3://C
                if (message.contains("[mirai:image:")) {
                    group.sendMessage("正在处理图片中...");
                    String url = StringUtils.substringBetween(message, "[mirai:image:", "]");
                    String imageUrl = group.getBot().queryImageUrl(MessageUtils.newImage(url));
                    InputStream imgio = botTools.SmartMattingB(imageUrl);
                    if(imgio!=null) {
                        botCreateCardsData.setImgid(imgio);
                        group.sendMessage("图片处理成功！\n请发送标题");
                        botCreateCardsData.setStep(4);
                    }else {
                        group.sendMessage("图片处理失败！");
                    }
                }
                break;
            case 4://D
                botCreateCardsData.setTitle(messageB);
                group.sendMessage("标题设置成功！\n请发送名字");
                botCreateCardsData.setStep(5);
                break;
            case 5://D
                BufferedImage imgio =  botTools.CreateCards(botCreateCardsData.getImgid(),messageB,botCreateCardsData.getTitle());
                Image img = group.uploadImage(imgio);
                group.sendMessage(new At(event.getSender()).plus(img));
                botCreateCardsData.setStep(0);
                break;
            default:
                break;
        }
    }


}
class BotCreateCardsData{
    private Boolean isRunning;
    private Long runningQQ;
    private Long runningGroup;
    private Integer step = 0;
    private InputStream imgid;
    private String title;

    public Boolean getRunning() {
        return isRunning;
    }

    public void setRunning(Boolean running) {
        isRunning = running;
    }

    public Long getRunningQQ() {
        return runningQQ;
    }

    public void setRunningQQ(Long runningQQ) {
        this.runningQQ = runningQQ;
    }

    public Long getRunningGroup() {
        return runningGroup;
    }

    public void setRunningGroup(Long runningGroup) {
        this.runningGroup = runningGroup;
    }

    public Integer getStep() {
        return step;
    }

    public void setStep(Integer step) {
        this.step = step;
    }

    public InputStream getImgid() {
        return imgid;
    }

    public void setImgid(InputStream imgid) {
        this.imgid = imgid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
